package com.augmentum.oes.dao.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.augmentum.oes.dao.QuestionDao;
import com.augmentum.oes.model.Pagination;
import com.augmentum.oes.model.Question;
import com.augmentum.oes.model.SortStatusEnum;

@Repository
public class QuestionDaoImpl extends BaseDaoImpl implements QuestionDao {

    private static final String MODEL_CLASS_NAME = Question.class.getName();
    private static final String QUERY_LIST_METHOD = ".queryList";
    private static final String GET_BY_ID = ".getById";
    private static final String UPDATE = ".update";
    private static final String DROP_BY_ID = ".dropById";
    private static final String SAVE = ".save";
    private static final String GET_COUNT = ".getCount";
    private static final String GET_LAST_QUESTION = ".getlastquestion";

    private static final String KEYWORD = "keyWord";
    private static final String OFFSET = "offset";
    private static final String MAXROW = "maxrow";
    private static final String SORT = "sort";
    private static final int MIN_PAGE = 1;
    private static final String GET_RANDOM_QUESTION = ".getRandomQuestion";

    public QuestionDaoImpl() {
        super();
    }

    @Override
    public Question save(Question question) {
        sqlSession.insert(MODEL_CLASS_NAME + SAVE, question);
        return question;
    }

    @Override
    public List<Question> getAllQuestion(Pagination pagination, SortStatusEnum sort, String keyWord) {
        pagination.setTotalCount(this.getTotalCount(keyWord));
        if (pagination.getCurrentPage() > pagination.getPageCount()) {
            pagination.setCurrentPage(pagination.getPageCount());
        }
        if (pagination.getCurrentPage() < MIN_PAGE) {
            pagination.setCurrentPage(MIN_PAGE);
        }
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put(KEYWORD, keyWord);
        parms.put(OFFSET, pagination.getOffset());
        parms.put(MAXROW, pagination.getPageSize());
        parms.put(SORT, sort.getValue());
        return sqlSession.selectList(MODEL_CLASS_NAME + QUERY_LIST_METHOD, parms);

    }

    @Override
    public Question getByQuestionId(int id) {
        return sqlSession.selectOne(MODEL_CLASS_NAME + GET_BY_ID, id);
    }

    @Override
    public void update(final Question question) {
        sqlSession.update(MODEL_CLASS_NAME + UPDATE, question);
    }

    @Override
    public void dropById(String[] idStr) {
        List<String> list = Arrays.asList(idStr);
        sqlSession.update(MODEL_CLASS_NAME + DROP_BY_ID, list);
    }

    @Override
    public int getTotalCount(String keyWord) {
        return sqlSession.selectOne(MODEL_CLASS_NAME + GET_COUNT, keyWord);
    }

    @Override
    public Question getLastQuestion() {
        return sqlSession.selectOne(MODEL_CLASS_NAME + GET_LAST_QUESTION);
    }

    public List<Question> getRandomQuestion(int row) {
        return sqlSession.selectList(MODEL_CLASS_NAME + GET_RANDOM_QUESTION, row);
    }
}
